package com.chenl.provider.题目.answer;

import lombok.SneakyThrows;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Day2 {
    @SneakyThrows
    public static void main(String[] args) {
        List<Integer> list=new ArrayList<>();
        list.add(1);//休眠1秒
        list.add(2);//休眠0.5秒
        list.add(3);//休眠2秒
        list.add(4);//休眠1.5秒
        ExecutorService executorService = Executors.newFixedThreadPool(list.size());
        List<Future>futures=new ArrayList<>();
        for (Integer integer : list) {
            switch (integer){
                case 1:
                    Future<?> future = executorService.submit(new MyRun(1000, integer));
                    futures.add(future);
                    break;
                case 2:
                    Future future1 = executorService.submit(new MyRun(500, integer));
                    futures.add(future1);
                    break;
                case 3:
                    Future future2 = executorService.submit(new MyRun(2000, integer));
                    futures.add(future2);
                    break;
                case 4:
                    Future future3 = executorService.submit(new MyRun(1500, integer));
                    futures.add(future3);
                    break;
            }
        }
        executorService.shutdown();
        List<String>resultList=new ArrayList<>();
        for (Future future : futures) {
            resultList.add(String.valueOf(future.get()));
        }
        System.out.println(resultList.toString());
    }
    public static class MyRun implements Callable {
        Integer sleepTime;
        Integer number;
        public MyRun(Integer sleepTime,Integer num){
            this.sleepTime=sleepTime;
            this.number=num;
        }
        @Override
        public Object call() {
            try {
                Thread.sleep(sleepTime);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(number);
         if (number==1){
             return "11";
         }else if (number==2){
             return "22";
         }else if (number==3){
             return "33";
         }else if (number==4){
             return "44";
         }else {
             return "666";
         }
        }
    }
}
